package 树;

/**
 * @author ssh
 * @date 2025/8/17
 * @description
 */
public class 将有序数组转位二叉排序树 {
    public TreeNode sortedArrayToBST(int[] nums) {
        return helper(nums,0,nums.length-1);
    }
    public TreeNode helper(int[] nums,int left,int right){
        if (left > right) {
            return null;
        }
        int mid=(left+right)/2;
        TreeNode root= new TreeNode(nums[mid]);
        root.left=helper(nums,0,mid-1);
        root.right=helper(nums,mid+1,right);
        return root;
    }

    public static void main(String[] args) {
        // 测试代码
        int[] nums = {-10, -3, 0, 5, 9};
        将有序数组转位二叉排序树 solution = new 将有序数组转位二叉排序树();
        TreeNode root = solution.sortedArrayToBST(nums);

        // 输出结果
        System.out.println(root.val); // 输出: 0
        System.out.println(root.left.val); // 输出: -10
        System.out.println(root.right.val); // 输出: 5
    }
}
